/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import me.zhengjie.modules.system.domain.Clazzandstudent;
import me.zhengjie.modules.system.domain.Studentnote;
import me.zhengjie.modules.system.domain.Studentsign;
import me.zhengjie.modules.system.domain.User;
import me.zhengjie.modules.system.mapper.ClazzandstudentMapper;
import me.zhengjie.modules.system.mapper.UserMapper;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.zhengjie.modules.system.service.StudentsignService;
import me.zhengjie.modules.system.domain.vo.StudentsignQueryCriteria;
import me.zhengjie.modules.system.mapper.StudentsignMapper;
import me.zhengjie.utils.SecurityUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import me.zhengjie.utils.PageUtil;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import me.zhengjie.utils.PageResult;

/**
* @description 服务实现
* @author shitou
* @date 2024-08-15
**/
@Service
@RequiredArgsConstructor
public class StudentsignServiceImpl extends ServiceImpl<StudentsignMapper, Studentsign> implements StudentsignService {

    private final StudentsignMapper studentsignMapper;

    private final ClazzandstudentMapper clazzAndStudentMapper;

    private final UserMapper userMapper;
    @Override
    public PageResult<Studentsign> queryAll(StudentsignQueryCriteria criteria, Page<Object> page){

        if(criteria.getIsAll() != 1){
            criteria.setIsOpen(null);
            criteria.setCreateUserId(Math.toIntExact(SecurityUtils.getCurrentUserId()));
        } else {
            QueryWrapper queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("userId",SecurityUtils.getCurrentUserId());
            Clazzandstudent clazzandstudent = clazzAndStudentMapper.selectOne(queryWrapper);

            QueryWrapper queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("clazzId",clazzandstudent.getClazzId());
            List<Clazzandstudent> clazzandstudentList = clazzAndStudentMapper.selectList(queryWrapper);
            List<Integer> userIds = new ArrayList<>();
            for(Clazzandstudent clazzStudent:clazzandstudentList){
                userIds.add(Math.toIntExact(clazzStudent.getUserId()));
            }
            criteria.setUserIds(userIds);
            criteria.setIsOpen(1);
        }
        return PageUtil.toPage(studentsignMapper.findAll(criteria, page));
    }

    @Override
    public List<Studentsign> queryAll(StudentsignQueryCriteria criteria){
        System.out.println("=criteria="+criteria.getIsAll());
        if(criteria.getIsAll() != 1){
//            criteria.setIsOpen(null);
            criteria.setCreateUserId(Math.toIntExact(SecurityUtils.getCurrentUserId()));
        } else {
            QueryWrapper queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("userId",SecurityUtils.getCurrentUserId());
            Clazzandstudent clazzandstudent = clazzAndStudentMapper.selectOne(queryWrapper);

            QueryWrapper queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("clazzId",clazzandstudent.getClazzId());
            List<Clazzandstudent> clazzandstudentList = clazzAndStudentMapper.selectList(queryWrapper1);
            List<Integer> userIds = new ArrayList<>();
            for(Clazzandstudent clazzStudent:clazzandstudentList){
                userIds.add(Math.toIntExact(clazzStudent.getUserId()));
            }
            criteria.setUserIds(userIds);
            criteria.setIsOpen(1);
        }


        List<Integer> allUserIds = new ArrayList<>();
        QueryWrapper queryWrapper1 = new QueryWrapper<>();


        List<Studentsign> studentsignList = studentsignMapper.findAll(criteria);
        for(Studentsign studentsign:studentsignList){
            allUserIds.add(studentsign.getCreateUserId());

        }
        queryWrapper1.in("user_id",allUserIds);
        List<User> userList = userMapper.selectList(queryWrapper1);
        for(Studentsign studentsign:studentsignList){
            for(User user:userList){
                if(studentsign.getCreateUserId() == Math.toIntExact(user.getId())){
                    studentsign.setCreateName(user.getUsername());
                    studentsign.setIcon(user.getAvatarPath());
                }
            }

        }
        return studentsignList;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(Studentsign resources) {
        resources.setCreateUserId(Math.toIntExact(SecurityUtils.getCurrentUserId()));
        save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Studentsign resources) {
        Studentsign studentsign = getById(resources.getId());
        studentsign.copy(resources);
        resources.setCreateUserId(Math.toIntExact(SecurityUtils.getCurrentUserId()));
        saveOrUpdate(studentsign);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Integer> ids) {
        removeBatchByIds(ids);
    }

    @Override
    public void download(List<Studentsign> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (Studentsign studentsign : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put(" teachid",  studentsign.getTeachId());
            map.put(" teachchapterid",  studentsign.getTeachChapterId());
            map.put(" teachsubsectionid",  studentsign.getTeachSubsectionId());
            map.put(" createuserid",  studentsign.getCreateUserId());
            map.put(" createtime",  studentsign.getCreateTime());
            map.put(" content",  studentsign.getContent());
            map.put(" coordinate",  studentsign.getCoordinate());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}